package com.youngball.web.webconfig;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by asus on 2016/6/12.
 */
public class BaseController {
    protected static HttpHeaders responseHeaders;
    static {

        responseHeaders = new HttpHeaders();
        responseHeaders.set("Access-Control-Allow-Origin", "*");
        responseHeaders.set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
        responseHeaders.set("Access-Control-Allow-Credentials", "true");
        responseHeaders.set("Access-Control-Max-Age", "60");
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map> operateExp(RuntimeException ex,HttpServletRequest request){
        Map maps=new HashMap();
        ex.printStackTrace();
        if ("com.airplane.baseservice.config.MyDefineException".equals(ex.getClass().getName())){
            maps.put("msg", ex.getMessage());
            maps.put("code", CodeMsg.CUSTOM.getValue());
        }else if("com.airplane.baseservice.exception.LoginException".equals(ex.getClass().getName())){
            maps.put("msg", "请先登录");
            maps.put("code", CodeMsg.UNAUTHORIZED.getValue());
        }else {
            maps.put("msg", "操作失败");
            maps.put("code", CodeMsg.ERROR.getValue());
        }
        return new ResponseEntity<Map>(maps, HttpStatus.OK);
    }

   public ResponseEntity<Map> bulidResult(){
       Map maps=new HashMap();
      maps.put("code",CodeMsg.SUC.getValue());
      maps.put("msg","操作成功");
       return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
    }

    public ResponseEntity<Map> bulidResult(String code,String msg,HttpStatus statusCode){
        Map maps=new HashMap();
        maps.put("code",code);
        maps.put("msg",msg);
        return new ResponseEntity<Map>(maps, responseHeaders, statusCode);
    }

    public ResponseEntity<Map> bulidResult(String code,String msg,HttpStatus statusCode,String enteredId){
        Map maps=new HashMap();
        maps.put("code",code);
        maps.put("msg",msg);
        maps.put("enteredId",enteredId);
        return new ResponseEntity<Map>(maps, responseHeaders, statusCode);
    }

    public ResponseEntity<Map> bulidResult(Object data){
        Map maps=new HashMap();
        maps.put("code",CodeMsg.SUC.getValue());
        maps.put("msg","操作成功");
        maps.put("data",data);
        return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.OK);
    }

    /**
     * 组装分页条件参数公共方法
     * @return
     */
    public Map<String,Object> assembledParameter(int pageIndex,int pageSize,Object...objects){
        Map<String,Object> map=new HashMap();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        return map;
    }

    /**
     * 组装分页条件参数公共方法
     * @return
     */
    public Map<String,Object> assembledParameter(String likeString,int pageIndex,int pageSize){
        Map<String,Object> map=new HashMap();
        map.put("likeString",likeString);
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        return map;
    }

    /**
     * 组装分页返回值json参数
     * @return
     */
    public LinkedHashMap<String,Object> assembledPagination(int pageindex,int pageSize,long totalItems){
        LinkedHashMap<String,Object> pagination = new LinkedHashMap<String,Object>();
        pagination.put("pageindex",pageindex);
        pagination.put("pageSize",pageSize);
        pagination.put("totalItems",totalItems);
        return pagination;
    }

 @InitBinder
    protected void initBinder(WebDataBinder binder) {
     // String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
     binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
         @Override
         public void setAsText(String text) {
             setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
         }

         @Override
         public String getAsText() {
             Object value = getValue();
             return value != null ? value.toString() : "";
         }
     });
 }

}
